﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using LibraryMusicStore;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraEditors.DXErrorProvider;

namespace AppMusicStore
{
    public partial class frmVAT : DevExpress.XtraEditors.XtraForm
    {
        IRepository<VAT> vatRep;

        public frmVAT()
        {
            InitializeComponent();
        }

        private void frmAlbumEditor_Load(object sender, EventArgs e)
        {
            try
            {
                btnCancel.CausesValidation = false;

                vatRep = new Repository<VAT>();

                RetriveData();
            }
            catch (Exception ex)
            {
                //Common.ShowErrorBox(ex.Message);
                throw;
            }
        }

        private void RetriveData()
        {
            try
            {
                var vatQuery = vatRep.GetById(1);
                calc.EditValue = vatQuery.Value;
            }
            catch (Exception)
            {
                throw;
            }
        }

        #region "Button click"
        private void btnAccept_Click(object sender, EventArgs e)
        {
            try
            {
                this.ValidateChildren();

                if (ErrorProvider1.HasErrors)
                {
                    foreach (var item in ErrorProvider1.GetControlsWithError())
                    {
                        item.Focus();
                        Common.ShowErrorBox(ErrorProvider1.GetError(item));
                        return;
                    }
                }

                var vatQ = vatRep.GetById(1);

                vatQ.Value = Convert.ToDecimal(calc.EditValue);

                vatRep.SubmitChanges();

                Common.ShowInfomationBox("Đã lưu");

                this.DialogResult = DialogResult.OK;
            }
            catch (Exception ex)
            {
                //Common.ShowErrorBox(ex.Message);
                throw;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            try
            {
                var vatQ = vatRep.GetById(1);
                if (CheckDiff(vatQ))
                {
                    DialogResult rs = Common.ShowQuestionBox("Lưu thay đổi ? ", "yesnocancel");
                    if (rs == DialogResult.Yes)
                        btnAccept.PerformClick();
                    else if (rs == DialogResult.Cancel)
                        return;
                }

            }
            catch (Exception ex)
            {
                //Common.ShowErrorBox(ex.Message);
                throw;
            }
            this.DialogResult = DialogResult.Cancel;
        }
        #endregion

        private bool CheckDiff(VAT vat)
        {
            if (vat.Value != Convert.ToDecimal(calc.EditValue))
            {
                return true;
            }

            return false;
        }

        private void Check(object sender, CancelEventArgs e)
        {
            string str = null;
            
            //title required
            if (sender.GetType() == typeof(CalcEdit))
            {
                CalcEdit calc = (CalcEdit)sender;
                if (calc.Text.Trim().Length == 0)
                    str = "Chưa nhập vat";
                else if (Convert.ToInt32(calc.EditValue) > 99 ||
                    Convert.ToInt32(calc.EditValue) < 1
                    )
                {
                    str = "0 < giá trị < 100";
                }
            }

            ErrorProvider1.SetIconAlignment((Control)sender, ErrorIconAlignment.MiddleRight);
            ErrorProvider1.SetError((Control)sender, str);
        }
    }
}